package com.service.tcp.Tcp_doing;

import com.alibaba.fastjson.JSON;
import com.domain.*;
import com.mapper.BoxHistory_mysql;
import com.mapper.Box_mysql;
import com.service.Public_funtion;
import com.service.tcp.Tcp_public_funtion;
import com.utilus.HttpRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.MysqlApplication.equip_list;
import static com.MysqlApplication.global_data;

@Service
public class KDG_to_Tcp_doing

{
    @Value(("${Manager_ip_addr}"))
    private String  manager_ip_addr;

    @Autowired
    private Public_funtion p;
    @Autowired
    private Tcp_public_funtion tcp_p;
    @Autowired
    private Box_mysql box_mysql;
    @Autowired
    BoxHistory_mysql boxHistory_mysql;
    @Autowired
    private HttpRequest httpRequest;
    @Autowired
    Phone_to_Tcp_doing p_to_t;

    public String doing (Tcp_public_data data,ChannelHandlerContext ctx)
    {
        if(data.getBatch()==1) {return batch_select1(data,ctx); }
        return "数据版本有误";
    }

    private String batch_select1(Tcp_public_data data,ChannelHandlerContext ctx)
    {
        if(data.getKind()==1) {return Kind_K1(data,ctx); }//发来注册信息
        if(data.getKind()==11) {return Kind_K11(data,ctx); }//发来为：打开柜门的文字请求信息
        if(data.getKind()==12) {return Kind_K12(data,ctx); }//发来为：快递柜发来验证码，要求打开柜门
        if(data.getKind()==15) {return Kind_K15(data,ctx); }//发来为：整体柜门开关状态的消息；
        return "数据种类有误";
    }

    private String Kind_K1(Tcp_public_data data,ChannelHandlerContext ctx)
    {//发来注册信息

//        Contents content=tcp_p.xml_to_Object(data.getContent());

        p.print_log ("接收到注册信息",6);
        String content=new String(data.content);
        Equip_data equip=tcp_p.Write_info(content,ctx);
        p.print_log("ip为"+ equip.getIP() +"名字为"+content+"的设备成功注册.",2);
        return "注册信息已经写入";

    }
    private String Kind_K11(Tcp_public_data data,ChannelHandlerContext ctx)
    {
        if(data.getOpen()==1) {return Kind_K11O1(data,ctx); }
        return "数据版本有误,无法执行";
    }
    private String Kind_K12(Tcp_public_data data,ChannelHandlerContext ctx)
    {
        if(data.getOpen()==1) { do_K12O1(data,ctx); }
        return "数据版本有误,无法执行";
    }
  private String  Kind_K15(Tcp_public_data data,ChannelHandlerContext ctx)
    {

        for (int i =0;i<global_data.door_buff_id.size();i++)
        {
            if ( Integer.valueOf(global_data.door_buff_id.get(i))==data.getSpeed())
            {
                global_data.door_buff_id.remove(i);
                global_data.door_buff.remove(i);
                break;
            }
        }
        global_data.door_buff_id.add(String.valueOf(data.getSpeed()));
        global_data.door_buff.add(data.content);

        httpRequest.SendPost2(manager_ip_addr+"/DoorBuff",global_data.door_buff.get(0),global_data.door_buff_id.get(0).getBytes());

        return "发来为：整体柜门开关状态的消息；";
    }

    private String Kind_K11O1(Tcp_public_data data,ChannelHandlerContext ctx)
    {
        if(data.getOpen()==1) {return do_K11O1(data,ctx); }
        return "数据版本有误,无法执行";
    }
    private String Open_K12O1S1(Tcp_public_data data,ChannelHandlerContext ctx)
    {
        if(data.getOpen()==1) { do_K12O1(data,ctx); }
        return "数据版本有误,无法执行";
    }

    private String do_K11O1(Tcp_public_data data,ChannelHandlerContext ctx)
    {//接收者的名字是注册的，有就发送，如果没有注册，就返回错误
        List<Contents> content=new ArrayList<>();
        String report="";
        try {
            content= tcp_p.xml_to_Object(new String(data.content));
            for(int i=0;i<content.size();i++)
            {
                int num=tcp_p.name_check_listequip(content.get(i).getName());
                if (num<0)
                {
                    report+=content.get(i).getName()+"未注册,未发送成功";
                }
                else
                {
                    ByteBuf encoded=tcp_p.tcp_to_bytebuf(ctx,data);
                    equip_list.get(num).getEquip().channel().writeAndFlush(encoded).syncUninterruptibly();
                }
            }
        } catch (DocumentException e) {
            p.print_log ("xml错误详细错误信息如下:"+ e,3);
            return  "xml格式错误";
        }
        Tcp_public_data send_data=tcp_p.equip_data_init();
        send_data.content=report.getBytes();
        ByteBuf encoded=tcp_p.tcp_to_bytebuf(ctx,send_data);
        ctx.channel().writeAndFlush(encoded).syncUninterruptibly();
        p.print_log(report,2);
        return "数据有误,无法执行";
    }

    private void do_K12O1(Tcp_public_data data,ChannelHandlerContext ctx)//通过快递柜机器打开柜门的消息
    {
        String report="";
        List<Box_data> LBoxdata=box_mysql.get_box(data.getClour());
        if(LBoxdata.size()>0)
        {
            String nowtime=p.get_DATE_String_HHmmss();
            Box_data box = JSON.parseObject(JSON.toJSONString(LBoxdata.get(0)), Box_data.class);
            if(box.getOuttime().compareTo(nowtime)>0)
            {
                BoxHistory_data boxh=new BoxHistory_data();
                p_to_t.doing(box);
                BeanUtils.copyProperties(box,boxh);
                boxh.setTime(nowtime);
                boxh.setOuttime(p.get_DATE_String_HHmmss());
                boxHistory_mysql.insert_boxhistory(boxh);
                report="请即使取走快递，并关闭柜门";
            }
            else
            {
                report="您的快递因长时间未取超时，请扫描下方二维码";
            }
        }
        else
        {
            report="验证码错误，请重新输入";
        }
        Tcp_public_data turn_data=tcp_p.equip_data_init();
        turn_data.setKind(13);
        turn_data.setOpen(2);
        turn_data.content=report.getBytes();
        ByteBuf encoded=tcp_p.tcp_to_bytebuf(ctx,turn_data);
        ctx.channel().writeAndFlush(encoded).syncUninterruptibly();
        p.print_log("快递柜发来验证码，打开柜门",1);
    }



}
